﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace z.v {
	/// <summary>
	/// Interaction logic for Product.xaml
	/// </summary>
	public partial class OrderBuy : UserControl {
		int pageNum = 0;
		int status = 0;
		const int ItemPerPage = 500;
		private int maxPageNum;
		private int modItemNum;
		public m.Producter curentProducter { get; set; }
		public OrderBuy() {
			InitializeComponent();
		}
		public IQueryable<m.Order> getDB() {
			//App.DB = new m.DataClassesDataContext(Properties.Settings.Default.ConnectSting);
			IQueryable<m.Order> ret = App.DB.Orders.Where(x=>x.Order_Type==128);
			switch(status){
				case 2: ret = ret.Where(x=>x.Date_X ==null) ; break;
				case 3: ret = ret.Where(x => x.Date_G == null);break;
				case 4: ret = ret.Where(x => x.Date_G != null && x.Date_T == null); break;
			}
			//if (status == 0) {
			//	miNew.Visibility = Visibility.Visible;
			//} else {
			//	miNew.Visibility = Visibility.Collapsed;
			//}
			if (curentProducter != null)
				ret = ret.Where(x => x.Producter == curentProducter);
			return ret;
		}
		public void refreshPages() {
			maxPageNum = (int)Math.Ceiling((float)(getDB().Count() / ItemPerPage));
			modItemNum = getDB().Count() % ItemPerPage;
			spPages.Children.Clear();
			for (int i = 1; i <= maxPageNum; i++) {
				var rb = new RadioButton();
				rb.Content = i;
				rb.Tag = i;
				rb.Checked += rb_Checked;
				spPages.Children.Add(rb);
			}
		}
		public void refreshDB() {
			int s = (maxPageNum - pageNum) * ItemPerPage - ItemPerPage + modItemNum;
			s = s < 0 ? 0 : s;
			tbl.ItemsSource = getDB().Skip(s).Take(ItemPerPage);
		}
		void rb_Checked(object sender, RoutedEventArgs e) {
			var skipIntNull = (sender as RadioButton).Tag as int?;
			pageNum = skipIntNull.HasValue ? skipIntNull.Value : 0;
			refreshDB();
		}
		private void Status_Checked(object sender, RoutedEventArgs e) {
			if (!int.TryParse((sender as RadioButton).Tag.ToString(), out status))
				status = 0;
			pageNum = 0;
			refreshPages();
			refreshDB();
		}
		private void _this_Loaded(object sender, RoutedEventArgs e) {
			refreshPages();
			refreshDB();
			tbl.Items.MoveCurrentToLast();
			if (tbl.Items.CurrentItem != null)
				tbl.ScrollIntoView(tbl.Items.CurrentItem);
		}
		private void tbl_LostFocus(object sender, RoutedEventArgs e) {
			App.DB.SubmitChanges();
		}
		private void miDetails_Click(object sender, RoutedEventArgs e) {
			if (!(tbl.SelectedItem is m.Order)) return;
			(tbl.SelectedItem as m.Order).viewBuyDetails();
			App.DB.SubmitChanges();
			updatePriceIn(tbl.SelectedItem as m.Order);
			//App.DB = new m.DataClassesDataContext();
			refreshDB();
		}
		public void updatePriceIn(m.Order o) {
			var mbr = System.Windows.MessageBox.Show("Yes/No","Cập nhật giá?", MessageBoxButton.YesNo);
			if (mbr.Equals(System.Windows.MessageBoxResult.Yes)) {
				foreach (m.Order_Detail x in o.Order_Details) {
					m.PriceIn pi = new m.PriceIn();
					pi.Date = DateTime.Now;
					pi.Producter = o.Producter;
					pi.Product = x.Product;
					pi.Price = x.Price;
					App.DB.PriceIns.InsertOnSubmit(pi);
				}
				App.DB.SubmitChanges();
			}
		}
		private void miNew_Click(object sender, RoutedEventArgs e) {
			var o = new m.Order();
			o.Date_N = DateTime.Now;
			o.Order_Type = 128;
			o.Employee_N = Main.GBUser;
			o.Producter = curentProducter;
			o.viewBuyDetails();
			if(!App.DB.Orders.Contains(o))
				App.DB.Orders.InsertOnSubmit(o);
			App.DB.SubmitChanges();
			updatePriceIn(o);
			//App.DB = new m.DataClassesDataContext(Properties.Settings.Default.ConnectSting);
			refreshDB();
		}
		public void setDate(int a) {
			var o = tbl.SelectedItem as m.Order;
			if (o.Date_N == null) o.Date_N = System.DateTime.Now;
			if (o.Employee_N == null) o.Employee_N = Main.GBUser;
			if (a < 2) return;
			if (o.Date_H == null) o.Date_H = System.DateTime.Now;
			if (a < 3) return;
			if (o.Date_X == null) o.Date_X = System.DateTime.Now;
			if (a < 4) return;
			if (o.Date_G == null) o.Date_G = System.DateTime.Now;
			if (o.Employee_G == null) o.Employee_G = Main.GBUser;
			if (a < 5) return;
			if (o.Date_T == null) o.Date_T = System.DateTime.Now;
			if (o.Employee_T == null) o.Employee_T = Main.GBUser;
			App.DB.SubmitChanges();
		}
		private void miGiao_Click(object sender, RoutedEventArgs e) {
			setDate(4);
		}
		private void miThu_Click(object sender, RoutedEventArgs e) {
			setDate(5);
		}
		private void btnSearch_Click(object sender, RoutedEventArgs e) {
			if (autProducter.Tag is m.Producter) {
				curentProducter = autProducter.Tag as m.Producter;
				refreshDB();
			}
		}
		private void btnClear_Click(object sender, RoutedEventArgs e) {
			curentProducter = null;
			autProducter.Tag = null;
			autProducter.txt.Text = "";
			refreshDB();
		}
		private void CommandBinding_Executed_1(object sender, ExecutedRoutedEventArgs e) {
			string str = e.Parameter as string;
			string[] astr = str.Split(',');
			int x;
			int.TryParse(astr[1], out x);
			((sender as OrderSale).tbl.SelectedItem as m.Order).Report(astr[0] == "1", x);
		}
	}
}
